﻿/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "Realmd/RealmList.h"
#include "Shared/Global_Macros.h"
#include "Shared/Base/LockGuard.h"
#include "Shared/Log/Log.h"
#include "Realmd/Global.h"
using namespace Shared;

RealmList::RealmList(void)
    : _realms()
    , _realmsLock()
{

}

RealmList::~RealmList(void)
{

}

void RealmList::update(void)
{
    SHARED_LOCK_GURAD_ASSERT(_realmsLock);

    _realms.clear();
    QueryResultPtr result(realmdSystem.loginDB().pquery("SELECT * FROM realmlist;"));
    if (result.get())
    {
        do
        {
            struct Realm realm = { 0 };
            realm.id = (*result)[0].toUInt32();
            realm.name = (*result)[1].toString();
            realm.address.append((*result)[2].c_str())
                .append(":").append((*result)[3].c_str());
            realm.icon = (*result)[4].toUInt8();
            realm.realmflags = (eRealmFlags)(*result)[5].toUInt8();
            realm.timezone = (*result)[6].toUInt8();
            realm.populationLevel = (*result)[8].toFloat();
            _realms.push_back(realm);
        } while (result->nextRow());
    }

    //sharedLog.outBasic("RealmList::update() load %d realms from database", _realms.size());
}

const RealmList::RealmArray RealmList::getRealms(void)
{
    SHARED_LOCK_GURAD_ASSERT(_realmsLock);
    return _realms;
}

void RealmList::clear(void)
{
    SHARED_LOCK_GURAD_ASSERT(_realmsLock);
    _realms.clear();
}